//
//  DGGNetworkHelper.m
//  DGGNetworkingSDK
//
//  Created by Dwt on 2019/12/25.
//

#import "DGGNetworkHelper.h"
#import <Reachability/Reachability.h>

@interface DGGNetworkHelper ()


@end

static NSString *const kCodeKey = @"code";

@implementation DGGNetworkHelper

+ (instancetype)shareInstance
{
    static DGGNetworkHelper *helper;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[DGGNetworkHelper alloc]init];
    });
    return helper;
}

- (instancetype)init
{
    if (self = [super init]) {
    }
    return self;
}

- (BOOL)isNetworkAvailable
{
    BOOL networkAvailabel = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    switch (reach.currentReachabilityStatus) {
        case NotReachable:
            networkAvailabel = NO;
            break;
        case ReachableViaWiFi:
            networkAvailabel = YES;
            break;
        case ReachableViaWWAN:
            networkAvailabel = YES;
            break;
    }
    return networkAvailabel;
}

#pragma mark - 网络错误
- (NSDictionary *)error:(DGGNetworkErrorMsgType)msgType;
{
    NSString *message = @"网络连接失败，请稍后重试";
    NSString *code = @"9999";
    switch (msgType) {
        case DGGNetworkErrorMsgTimeout:
            message = @"网络超时，请稍后重试";
            code = @"9997";
            break;
            
        case DGGNetworkErrorMsgCancel:
            message = @"网络连接失败，请稍后重试";
            code = @"9996";
            break;
            
        case DGGNetworkErrorMsgFailed:
            message = @"网络错误，请稍后重试";
            code = @"9998";
            break;
            
        default:
            break;
    }
    return @{kCodeKey : code, self.messageKey : message};
}


@end
